// pages/index/brandlist/brandlist.js
import { axios } from '../../../utils/ajax.js'
import { checklogin } from '../../../utils/login.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brandid:'',
    goodslist:[],
    page: 1,
    totalpages: '',
    userid:'',
    token:'',
    canload: false,
    data:'',
    ishelped:'',
    //筛选总数据
    filterData:'',
    //排序方式 0 默认  1 高价  2 低价 3 人气
    sortType: 0 ,   
    attrType: '',          //4|1
    attrTypeId: '',         //-1:137|38,39,40,41,42,
    //展示筛选数据
    selectedData:[],
    //最大助力人数
    maxNum:'',
    //当前助力人数
    likeNum:'',
    //是否助力过
    isLike:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { userid = '', token = '' } = wx.getStorageSync('userinfo') 
    console.log(options.brandid)
    let brandid = options.brandid
    this.setData({
      brandid: brandid,
      userid: userid,
      token: token
    })
    //获取页面数据
    this.getdata()
    //获取筛选列表信息
    axios({
      url: '/goods/filterGoods',
      data: {
        brandId: this.data.brandid
      }
    }).then((res)=>{
      console.log(res)
      this.setData({
        filterData: res.data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page
    let totalpages = this.data.totalpages
    console.log(page)
    console.log(totalpages)
    if (page < totalpages){
      page++
      this.setData({
        page: page
      })
      this.getdata(true)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //获取数据
  getdata(isreachbottom = false){
    axios({
      url: '/fashion/goodslist',
      data: {
        brandId: this.data.brandid,
        page: this.data.page,
        userid: this.data.userid,
        token: this.data.token,
        sortType: this.data.sortType,
        attrType: this.data.attrType,
        attrTypeId: this.data.attrTypeId,
        userid: this.data.userid,
        token: this.data.token
      }
    }).then((res) => {
      console.log(res)
      let newdata = res.data.data
      newdata.map(x => x.crazyPrice = parseInt(x.crazyPrice) )
      newdata.map(x => x.shopPrice = parseInt(x.shopPrice))
      wx.setNavigationBarTitle({
        title: res.data.title
      })
      //触底事件 拼接数据
      if (isreachbottom){
        this.setData({
          goodslist: this.data.goodslist.concat(newdata)
        })
      //更变数据 直接改变
      }else{
        this.setData({
          goodslist: newdata
        })
      }
      this.setData({
        totalpages: res.data.totalPages,
        data: res.data,
        maxNum: parseInt(res.data.maxNum),
        likeNum: parseInt(res.data.likeNum),
        isLike: res.data.isLike
      })
      if (this.data.page < this.data.totalpages){
        this.setData({
          canload: true
        })
      }else{
        this.setData({
          canload: false
        }) 
      }
    })
  },
  //点击助力
  tohelp () {
    checklogin().then((res) => {
      let hascollected = this.data.isLike
      console.log(hascollected)
      if (!hascollected){
        axios({
          url: '/brand/collect',
          data: {
            userid: res.userid,
            token: res.token,
            brandId: this.data.brandid,
            type: 1
          }
        }).then((res) => {
          console.log(res)
          this.setData({
            isLike: !this.data.isLike,
            likeNum: this.data.likeNum+1
          })
          wx.showToast({
            title: res.data.message,
            icon: "none"
          })
        })
      }else{
        wx.showToast({
          title: '已经助力过',
          icon:"none"
        })
      }
    })
  },
  //筛选数据
  changeFilterData (data){
    console.log(data)
    this.setData({
      filterData:data
    })
    //自定义筛选数据
    let selectedData = []
    data.forEach((value,index)=>{
      selectedData[index] = {}
      selectedData[index].name = value.name
      let attr = []
      value.attr.forEach((val,num)=>{
        if (val.selected){
          attr.push(val)
        }
      })
      selectedData[index].attr = attr
    })
    console.log(selectedData)

    let attrType = this.data.attrType
    attrType = data.map(x=>x.type).join('|')
    let attrTypeId = selectedData.map(x => x.attr) 
    attrTypeId.forEach((value,index)=>{
      attrTypeId[index] = value.map(x=>x.id).join(',')
    })
    attrTypeId = attrTypeId.join('|')
    console.log(attrTypeId )
    this.setData({
      selectedData: selectedData,
      attrType: attrType,
      attrTypeId: attrTypeId
    })
    this.getdata()
  },
  //获取组件传递上来的数据
  filterComponentChange (e){
    //获取排序数据
    console.log(e.detail.sorttype)
    this.setData({
      sortType: e.detail.sorttype
    })
    this.getdata()
  },
  //关闭筛选
  clearchoose () {
    this.setData({
      selectedData: [],
      attrType: '',
      attrTypeId: ''
    })
    this.getdata()
  }
})